//
//  MineViewController.m
//  LuckyRadio
//
//  Created by Mr、J on 15/11/25.
//  Copyright © 2015年 chenjun. All rights reserved.
//

#import "MineViewController.h"
#import "CoreDataManager.h"
#import "Song.h"

#define WIDTH [UIScreen mainScreen].bounds.size.width/375
#define HEIGHT [UIScreen mainScreen].bounds.size.height/667

@interface MineViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, retain)UISegmentedControl *segmentedControl;

@property (nonatomic, retain)NSMutableArray *arrayDiscover;

@property (nonatomic, retain)UITableView *tableView;

@property (nonatomic, retain)CoreDataManager *coreDataManager;

@end

@implementation MineViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"我的收藏";
    
    self.coreDataManager = [CoreDataManager shareManager];
    NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Song"];
    NSArray *array_result = [self.coreDataManager.managedObjectContext executeFetchRequest:request error:nil];
    
    self.arrayDiscover = [NSMutableArray arrayWithArray:array_result];
    NSLog(@"%ld",self.arrayDiscover.count);
    [self createSubviews];
    
    
    
    [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cellIdentify"];
    
}

- (void)createSubviews
{
    UIButton *btnDelect = [UIButton buttonWithType:UIButtonTypeCustom];
    btnDelect.frame = CGRectMake( 0, 0, 50 * WIDTH, 50 * HEIGHT);
    [btnDelect setTitle:@"编辑" forState:UIControlStateNormal];
    [btnDelect setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [btnDelect addTarget:self action:@selector(btnDelectAction:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btnDelect];

    _segmentedControl = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"01",@"02",@"03", nil]];
    _segmentedControl.frame = CGRectMake(0, 64, self.view.frame.size.width, 30 * HEIGHT);
    _segmentedControl.tintColor = [UIColor redColor];
    _segmentedControl.selectedSegmentIndex = 0;
    [_segmentedControl addTarget:self action:@selector(segmentedControlAction:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:_segmentedControl];
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 30 * HEIGHT + 64, self.view.frame.size.width, self.view.frame.size.height - 30 * HEIGHT + 64) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self setExtraCellLineHidden:_tableView];
    [self.view addSubview:_tableView];
}
-(void)setExtraCellLineHidden: (UITableView *)tableView
{
    UIView *view = [UIView new];
    view.backgroundColor = [UIColor clearColor];
    [tableView setTableFooterView:view];
    [view release];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.arrayDiscover.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 45 * HEIGHT;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellIdentify"];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cellIdentify"];
    }
    cell.textLabel.text = [self.arrayDiscover[indexPath.row] songName];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleDelete;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Song"];
        request.predicate = [NSPredicate predicateWithFormat:@"songName = %@",[self.arrayDiscover[indexPath.row] songName]];
        NSArray *array = [_coreDataManager.managedObjectContext executeFetchRequest:request error:nil];
        for (Song *song in array) {
            [_coreDataManager.managedObjectContext deleteObject:song];
        }
        [_coreDataManager saveContext];
        [self.arrayDiscover removeObjectAtIndex:indexPath.row];
        [_tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationRight];
        [_tableView reloadData];
    }
}
- (void)btnDelectAction:(UIButton *)btn
{
    btn.selected = !btn.isSelected;
    if (btn.selected) {
        [btn setTitle:@"完成" forState:UIControlStateNormal];
        [_tableView setEditing:YES animated:YES];
    }else
    {
        [btn setTitle:@"编辑" forState:UIControlStateNormal];
        [_tableView setEditing:NO animated:YES];
    }
}

- (void)segmentedControlAction:(UISegmentedControl *)segmented
{
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
